package com.kingtao.session;

import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: kingtao
 * @Date: 2023-08-25-14:36
 * @Description:
 */
public class GroupSessionImpl implements GroupSession {

    private final Map<Integer, Channel> idToChannel = new ConcurrentHashMap<>();
    private final Map<Channel, Integer> channelToId = new ConcurrentHashMap<>();

    @Override
    public void bind(Channel channel, int master) {
        idToChannel.put(master, channel);
        channelToId.put(channel, master);
    }

    @Override
    public void unbind(Channel channel) {
        Integer master = channelToId.remove(channel);
        idToChannel.remove(master);
    }


    @Override
    public Channel getChannel(int id) {
        return idToChannel.get(id);
    }

    @Override
    public int getChannelNum() {
        return idToChannel.size();
    }

    @Override
    public String toString() {
        return idToChannel.toString();
    }
}
